# This file is used to get the views from couchdb

import couchdb
import json
import fanAlgorithm

def resultAsListKey(viewResult):
    # return the result of a "view query" as a list of dictionary
    result = []
    for doc in viewResult:
        result.append(doc.key)
    return result

def resultAsListValue(viewResult):
    # return the result of a "view query" as a list of dictionary
    result = []
    for doc in viewResult:
        result.append(doc.value)
    return result

def resultAsJSONList(viewResult):
    # return the list of result as a json list
    return json.dumps([doc for doc in viewResult])

def resultAsListAtTime(viewResult, time):
    # return the list of results correspondent to a given time
    result = []
    for doc in viewResult:
        if(doc.key.startswith(time)):
            result.append(doc.value)
    return result

################################## Commits #####################################################################

def getCommitView(serverName, databaseName):
    # get the result from the commit view for the given database and server
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/commitview/_view/commitview')
    return json.dumps(resultAsListKey(v))

def getSmallCommitView(serverName, databaseName):
    # get the result from the commit view for the given database and server
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallcommitview/_view/smallcommitview')
    return json.dumps(resultAsListValue(v))

def getMonthCommitView(serverName, databaseName):
    # get the result from the commit view grouped by month
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/commitMonthCount/_view/commitMonthCount',group=True))

def getYearCommitView(serverName, databaseName):
    # get the result from the commit view grouped by year
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/commitYearCount/_view/commitYearCount',group=True))

def getDayCommitView(serverName, databaseName):
    # get the result from the commit view grouped by day
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/commitDayCount/_view/commitDayCount',group=True))

def getCommitForMonth(serverName, databaseName, month):
    # get the commits made in a given month
    # month is something like YYYY-MM
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallcommitview/_view/smallcommitview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, month))
    
def getCommitForYear(serverName, databaseName, year):
    # get the commits made in a given year
    # where year is something like YYYY
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallcommitview/_view/smallcommitview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, year))

def getCommitForDay(serverName, databaseName, day):
    # get the commits made in a given day
    # where year is something like YYYY-MM-dd
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallcommitview/_view/smallcommitview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, day))
################################################################################################################

#################################### Emails ####################################################################

def getSmallEmailView(serverName, databaseName):
    # get the result from the commit view for the given database and server
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallemailview/_view/smallemailview')
    return json.dumps(resultAsListValue(v))

def getMonthEmailView(serverName, databaseName):
    # get the result from the commit view grouped by month
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/emailMonthCount/_view/emailMonthCount',group=True))

def getYearEmailView(serverName, databaseName):
    # get the result from the commit view grouped by year
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/emailYearCount/_view/emailYearCount',group=True))

def getDayEmailView(serverName, databaseName):
    # get the result from the commit view grouped by day
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/emailDayCount/_view/emailDayCount',group=True))

def getEmailForMonth(serverName, databaseName, month):
    # get the commits made in a given month,
    # month is something like YYYY-MM
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallemailview/_view/smallemailview')
    #return json.dumps(resultAsListAtTime(v, month))
    return json.dumps(fanAlgorithm.fanSearch(v.rows, month))

def getEmailForYear(serverName, databaseName, year):
    # get the emails made in a given year
    # where year is something like YYYY
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallemailview/_view/smallemailview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, year))

def getEmailForDay(serverName, databaseName, day):
    # get the emails made in a given day
    # where year is something like YYYY-MM-dd
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/smallemailview/_view/smallemailview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, day))
################################################################################################################

################################ Issues ########################################################################

def getIssueView(serverName, databaseName):
    # get the result form the issue view for the given database and server
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/issueview/_view/issueview')
    return json.dumps(resultAsListValue(v))

def getMonthIssueView(serverName, databaseName):
    # get the result from the commit view grouped by month
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/issueMonthCount/_view/issueMonthCount',group=True))

def getYearIssueView(serverName, databaseName):
    # get the result from the commit view grouped by year
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/issueYearCount/_view/issueYearCount',group=True))

def getDayIssueView(serverName, databaseName):
    # get the result from the commit view grouped by day
    server = couchdb.Server(serverName)
    db = server[databaseName]
    return resultAsJSONList(db.view('_design/issueDayCount/_view/issueDayCount',group=True))

def getIssueForMonth(serverName, databaseName, month):
    # get the commits made in a given month,
    # month is something like YYYY-MM
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/issueview/_view/issueview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, month))

def getIssueForYear(serverName, databaseName, year):
    # get the issues made in a given year
    # where year is something like YYYY
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/issueview/_view/issueview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, year))

def getIssueForDay(serverName, databaseName, day):
    # get the issues made in a given day
    # where year is something like YYYY-MM-dd
    server = couchdb.Server(serverName)
    db = server[databaseName]
    v=db.view('_design/issueview/_view/issueview')
    return json.dumps(fanAlgorithm.fanSearch(v.rows, day))
################################################################################################################

def getDocumentById(serverName, databaseName, id):
    # get the document with the given id from the database
    server = couchdb.Server(serverName)
    db = server[databaseName]
    doc = db[id]
    return doc 
